package com.hsav.kcxb.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hsav.kcxb.R;
import com.hsav.kcxb.activity.UserCenterActivity;
import com.hsav.kcxb.fragment.my.MyFrinendFavFragment;
import com.hsav.kcxb.fragment.user_center.WorkFragment;
import com.kcbsdk.EventHandler;

import java.util.ArrayList;

import static com.hsav.kcxb.main.MSYS.MSTRING;
import static com.hsav.kcxb.main.MSYS.mMyManage;
import static com.hsav.kcxb.main.MSYS.mMyTools;

/**
 * Created by zhao on 16-12-8.
 */

public class UserFragment extends Fragment {

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private String[] mTitles = {"好友推荐", "好友作品", "我的作品", "我的收藏"};
    private ArrayList<Fragment> mFrgmentList = new ArrayList<>();
    private TextView mTitleTv;
    private ImageView mPlayingImg;
    private ImageView mPauseImg;
    private ImageView mMyImg;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.frag_user_layout, container, false);
        initView(rootView);
        return rootView;
    }
    public void OnShowWindow(Activity activity, boolean show){
        if (show && mMyManage != null){
//            mMyTools.Log("onCreateView initKcbData "+mActivity);
            mMyManage.setUiEventCallBack(activity, new EventHandler(){
                @Override
                public void onComplete(int methodType, boolean isFail, String message){
                    if (methodType > 0){
                        if (isFail){
                            mMyManage.Toast(String.format("%s%s(%08x)", getString(R.string.methodError), message, methodType));
                        }
                        else {
                            if (mMyManage.isSucessLoginSvr(methodType)){
                                MSTRING(String.format("mMyManage.isEventLoginSucess"));
                                //mMyManage.mSvrManage.checkLoginSvr();
                            }
                        }
                    } else{

                    }
                }
            });
//            mMyManage.loginSvr("13715181539", "c27916786", "8888");

        }
    }

    private void initView(View rootView) {
        mTitleTv = (TextView)rootView.findViewById(R.id.tv_top_title);
        mTitleTv.setText("我");
        mPlayingImg = (ImageView)rootView.findViewById(R.id.iv_head_playing);
        final AnimationDrawable animationDrawable = (AnimationDrawable)mPlayingImg.getDrawable();
        animationDrawable.start();
        mPauseImg = (ImageView)rootView.findViewById(R.id.iv_head_pause);

        mMyImg = (ImageView)rootView.findViewById(R.id.iv_my_photo);
        mMyImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), UserCenterActivity.class);
                startActivity(intent);
            }
        });

        mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager_my);
        mTabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_my);
//        WorkFragment workFragment = WorkFragment.newInstance("","");
//        mFrgmentList.add(workFragment);
//        mFrgmentList.add(workFragment);
//        mFrgmentList.add(workFragment);
//        mFrgmentList.add(workFragment);
        mViewPager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                if(position==0){
                    return new MyFrinendFavFragment();
                }
                return WorkFragment.newInstance(position);
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);

    }
}
